package com.bjsxt.back.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.action.BaseAction;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.user.pojo.AAdmin;
import com.bjsxt.user.service.IService;
@Component
@RequestMapping("back")
public class AdminBackAction extends BaseAction
{
	@Resource
	private IService service;
	@RequestMapping("/main.htm")
	public String showMain()
	{
		System.out.println("-----------------mian------------------");
		return "back/main";
	}
	@RequestMapping(value="/adminInsertSubmit.htm",produces="text/html;charset=UTF-8")
	public @ResponseBody String adminInsertSubmit(HttpServletRequest req,AAdmin admin)
	{
		admin.setCreateTime(new Date());
		admin.setLastLoginTime(new Date());
		admin.setUpdateTime(new Date());
		System.out.println(admin+"---------------------");
		/* 执行插入操作 */
		JSONObject json = service.insertAdmin(admin);
		String info =json.getString("info");
		json=this.returnJuiJSON(req, info);
		System.out.println("rel:"+json.getString("rel"));
		return json.toJSONString();
	}
	/**
	 * 添加管理员
	 * @return
	 */
	@RequestMapping("/adminInsert.htm")
	public String adminInsert()
	{	
		
		System.out.println("---------------adminInsert------------");
		return "back/adminInsert";
	}
	/**
	 * 用户列表
	 * @param req
	 * @return
	 */
	@RequestMapping("/adminList.htm")
	public String adminList(HttpServletRequest req)
	{	
		HashMap condMap = new HashMap();
		/* 接收参数 */
		String keyword = req.getParameter("keyword");
		String status = req.getParameter("status");
		String endTime = req.getParameter("endTime");
		String startTime = req.getParameter("startTime");
		String currentPage=req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		if(keyword != null)
		{
			condMap.put("keyword", keyword);
		}
		if(status != null)
		{
			condMap.put("status", status);
		}
		if(endTime != null && !"".equals(endTime))
		{
			/* 将时间字符串转换成日期 */
			Date endDate = this.dateFormatUtil.format(endTime, "yyyy-MM-dd HH:mm:ss");
			condMap.put("endTime", endDate);
		}
		if(startTime != null && !"".equals(startTime))
		{
			/* 将时间字符串转换成日期 */
			Date startDate = this.dateFormatUtil.format(startTime, "yyyy-MM-dd HH:mm:ss");
			condMap.put("startTime", startDate);
		}
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		if(currentPage != null && pageSize !=null)
		{
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		}
		List adminList = service.findListAdmin(pageInfoUtil, condMap);
		req.setAttribute("adminList", adminList);
		req.setAttribute("status", status);
		req.setAttribute("keyword", keyword);
		req.setAttribute("endTime", endTime);
		req.setAttribute("startTime", startTime);
		req.setAttribute("pageInfoUtil", pageInfoUtil);
		return "back/adminList";
	}
	
	/**
	 * 退出
	 * @param req
	 * @return
	 */
	@RequestMapping("/loginOut.htm")
	public String loginOut(HttpServletRequest req)
	{
		req.getSession().removeAttribute("admin");
		req.getSession().removeAttribute("lastLoginTime");
		System.out.println("------------退出-------------");
		return "redirect:http://localhost:8080/loan/noLogin/login.htm";
	}
	/**
	 * 打开leftMenu
	 * @return
	 */
	@RequestMapping("/leftMenu.htm")
	public String leftMenu()
	{
		System.out.println("leftMenu");
		
		return "back/leftMenu";
	}
}
